package com.luke.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.luke.springcloud.inter.PaymentServiceFeign;
import com.luke.springcloud.service.SentinelTestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/consumer")
@Slf4j
public class SentinelTestController {

    @Autowired
    private SentinelTestService sentinelTestService;

    @Resource
    private PaymentServiceFeign paymentServiceFeign;

    @GetMapping(value = "/hello/{name}")
    public String apiHello(@PathVariable String name) {
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return sentinelTestService.sayHello(name);
    }

    @GetMapping(value = "/testA")
    public String testA() {
        log.info("testA()");
        return "hi,A";
    }

    @GetMapping(value = "/testB")
    public String testB() {
        log.info("testB()");
        return sentinelTestService.sayHello("testB");
    }

    @GetMapping(value = "/testC")
    public String testC() {
        log.info("testC()");
        return sentinelTestService.sayHello("testC");
    }

    //------------------------熔断降级--------------------------------------

    @GetMapping(value = "/testD")
    public String testD() {//测试RT
        log.info("testRT()");
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "hi,testD";
    }

    @GetMapping(value = "/testE")
    public String testE() {//测试异常比例
        log.info("test异常比例()");
        int i = 10/0;
        return "hi,testE";
    }


    @GetMapping(value = "/testF")
    public String testF() {//测试异常数
        log.info("test异常数()");
        int i = 10/0;
        return "hi,testF";
    }

    //------------------------热点规则--------------------------------------
    //热点规则不使用@SentinelResource，触发之后不会返回sentinel的友好提示Blocked by Sentinel (flow limiting)，
    // 而是直接返回异常信息，所以建议使用@SentinelResource返回自定义响结果
    @GetMapping("/testHotKey")
    @SentinelResource(value = "hotKey",blockHandler = "testHotKeyblockHandler")
    public String testHotKey(@RequestParam(value = "p1",required = false) String p1,
                             @RequestParam(value = "p2",required = false) String p2){
        //int a = 10/0;//资源请求又异常的话，在阀值内，会返回异常信息，超过阀值则交给testHotKeyblockHandler处理返回限流值
        return "testHotKey";
    }

    public String testHotKeyblockHandler(String p1, String p2, BlockException blockException){
        log.info("p1:{},p2:{},exception:{}",p1,p2,blockException);
        return "testHotKeyblockHandler";
    }

    //-----------------------------sentinel整合openfeign-------------------------------------------

    @GetMapping("/payment/testSentinel")
    public String testSentinel(@RequestParam("name") String name){
        return paymentServiceFeign.testSentinel(name);
    }

}
